package com.xiaowu.xblog.web;

import com.xiaowu.xblog.constant.MessageConstant;
import com.xiaowu.xblog.dto.UserCreateDTO;
import com.xiaowu.xblog.dto.UserLoginDTO;
import com.xiaowu.xblog.entity.SysUser;
import com.xiaowu.xblog.model.UserLoginVO;
import com.xiaowu.xblog.model.UserVO;
import com.xiaowu.xblog.service.SysUserService;
import com.xiaowu.xblog.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;


import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.startup.Tomcat;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description:
 * @author: xiaowu
 * @time: 2025/6/24 23:04
 */

@RestController
@RequestMapping("/sysuser")
@Tag(name = "用户相关接口")
@Slf4j
public class SysUserController extends BaseController<SysUserService, SysUser> {

    public SysUserController(SysUserService service) {
        super(service); // 调用父类构造函数注入 service
    }
    // 如果有自定义接口，可在这里扩展
    @Operation(summary = "用户登录接口")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        return Result.success(service.login(userLoginDTO));
    }

    @Operation(summary = "用户注册接口")
    @PostMapping("/register")
    public Result<Object> insertUser(@RequestBody UserCreateDTO userCreateDTO) {
        int result = service.insertUser(userCreateDTO);
        if (result != 0) {
            return Result.success(MessageConstant.REGISTER_SUCCESS);
        } else {
            return Result.error(MessageConstant.REGISTER_FAIL);
        }
    }
    @Operation(summary = "用户信息回显")
    @GetMapping("/one")
    public Result<UserVO> getUserInfo(@RequestParam(name = "id") Long id){
        UserVO userVO = service.getUserById(id);

        return Result.success(userVO);
    }

    @Operation(summary = "用户修改信息")
    @PostMapping("/update")
    public Result<Object> updateUser(@RequestBody UserCreateDTO userDTO) {
        int update = service.updateUser(userDTO);
        if (update == 0) {
            return Result.error(MessageConstant.UPDATE_FAIL);
        }
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }

    @Operation(summary = "GitHub 第三方登录接口")
    @PostMapping("/github")
    public Result<Object> githubLogin(@RequestBody Map<String, String> payload) {
        String code = payload.get("code");
        return service.loginByGithubCode(code);
    }
}
